<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Administrator extends MY_AdminController
{
    public static $_BasePath;
    public static $_ImagePath;

    function  __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('security');
        $this->load->helper('url');
        $this->load->helper('myHelper');
        $this->_CheckLogin();
        Administrator::$_BasePath = base_url(get_class());
        Administrator::$_ImagePath = base_url('assets/skins/admin/image/');
    }

    private function _CheckLogin()
    {
        $uri = strtolower(uri_string());
        $class = strtolower(get_class());
        $this->load->model(MY_AdminController::MODEL_FOLDER . 'modelLogin');
        $userInfo = $this->session->userdata(MY_AdminController::USER_INFO);
        $loginSearch = strpos($uri, $class . '/login');
        if (is_array($userInfo) && $this->modelLogin->checkLogin($userInfo)) {
            if ($loginSearch === 0 || $loginSearch === 1) {
                redirect(base_url(get_class() . '/Home'));
            }
            return;
        }
        if ($loginSearch !== 0 && $loginSearch !== 1) {
            $this->session->sess_destroy();
            redirect(base_url(get_class() . '/Login'));
        }
    }

    private function _Template($name, $data = array())
    {
        $this->load->library('admin/Wp_Header');
        $this->load->view(MY_AdminController::VIEW_FOLDER . "header", array(
                'style' => $this->wp_header->LoadCss(MY_AdminController::CSS_FOLDER, array(
                        'dashicons',
                        'admin-bar',
                        'wp-admin',
                        'buttons',
                        'wp-auth-check'
                    )),
                'javascript' => $this->wp_header->LoadJs(MY_AdminController::JAVASCRIPT_FOLDER, array(
                        'jquery-1.7.1',
                        'jquery-migrate',
                        'json2',
                        'utils'
                    )),
                'themeClass' => 'theme-color5'
            ));
        $this->load->view(MY_AdminController::VIEW_FOLDER . $name, $data);
        $this->load->view(MY_AdminController::VIEW_FOLDER . "footer");
    }

    /*----------------------------Login------------------------------------*/
    public function Index()
    {
        redirect(base_url(get_class() . '/Login'));
    }

    public function Login()
    {
        $message = '';
        if (
            FALSE !== ($submit = $this->input->post('submit', TRUE)) &&
            FALSE !== ($username = $this->input->post('username', TRUE)) &&
            FALSE !== ($password = $this->input->post('password', TRUE))
        ) {
            $this->load->model(MY_AdminController::MODEL_FOLDER . 'modelLogin');
            $userInfo = $this->modelLogin->Login($username, $password);
            if (is_array($userInfo)) {
                $this->session->set_userdata(MY_AdminController::USER_INFO, $userInfo);
                if ($this->input->is_ajax_request()) {
                    die(json_encode(array(
                        'status' => TRUE,
                        'code' => 'redirect',
                        'content' => base_url(get_class() . '/Home')
                    )));
                }
                redirect(base_url(get_class() . '/Home'));
            } else {
                $this->session->unset_userdata(MY_AdminController::USER_INFO);
                $message = $userInfo;
                if ($this->input->is_ajax_request()) {
                    die(json_encode(array(
                        'status' => FALSE,
                        'code' => 'error',
                        'content' => $message
                    )));
                }
            }
        }
        $this->load->view(MY_AdminController::VIEW_FOLDER . "header", array(
                'style' => ShowStyle(array('font-awesome.min.css', 'font-awesome-ie7.min.css', 'login.css'), 'assets/skins/admin/css/'),
                'javascript' => ShowJavascript(array('jquery-1.7.1.min.js', 'login.js'), 'assets/skins/admin/js/')
            ));
        $this->load->view(MY_AdminController::VIEW_FOLDER . "login", array(
                'action' => current_url(),
                'message' => $message,
            ));
        $this->load->view(MY_AdminController::VIEW_FOLDER . "footer");
    }

    public function Logout()
    {
        $this->session->unset_userdata(MY_AdminController::USER_INFO);
        redirect(base_url(get_class() . '/Login'));
    }

    /*--------------------------Dashboard---------------------------------*/
    public function Home()
    {
        $this->_Template('home');
    }

    public function Test()
    {
        echo APPPATH;
    }
}